/* 
 * The MIT License
 *
 * Copyright 2014 Venkat Ram Akkineni.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.netbeans.modules.tfsnb.tfvc.workspace;

import com.microsoft.tfs.core.TFSTeamProjectCollection;
import com.microsoft.tfs.core.clients.versioncontrol.VersionControlClient;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Item;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.ItemSet;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.RecursionType;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.WorkingFolder;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JToolBar;
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.modules.tfsnb.core.ConnectionService;
import org.netbeans.modules.tfsnb.core.ConnectionSettings;
import org.netbeans.modules.tfsnb.core.tabs.TabPanel;
import org.netbeans.modules.tfsnb.core.tabs.TabPanelCallback;
import org.netbeans.modules.tfsnb.core.tabs.TabPanelDescription;
import org.openide.awt.UndoRedo;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;

/**
 *
 * @author Venkat Ram Akkineni
 */
public class WorkspacesMultiView implements TabPanel {
    
    @Override
    public String getId() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public TabPanelDescription getMultiViewPanelDescription() {
        return multiViewDescription;
    }

    @Override
    public void setMultiViewPanelDescription(TabPanelDescription desc) {
        this.multiViewDescription = desc;
    }

    @Override
    public void setLookup(Lookup lookup) {
        this.lookup = lookup;
    }

    @Override
    public JComponent getVisualRepresentation() {
        return configuredWorkspacesTablePanel;
    }

    @Override
    public JComponent getToolbarRepresentation() {
        return configuredWorkspacesTablePanel.getToolBar();
    }

    @Override
    public Action[] getActions() {
        return new Action[0];
    }

    @Override
    public Lookup getLookup() {
        return this.lookup;
    }

    @Override
    public void componentOpened() {
        
    }

    @Override
    public void componentClosed() {
        
    }

    @Override
    public UndoRedo getUndoRedo() {
        return UndoRedo.NONE;
    }

    @Override
    public void setMultiViewCallback(TabPanelCallback callback) {
        this.multiViewPanelCallback = callback;
    }

    @Override
    public CloseOperationState canCloseElement() {
        return CloseOperationState.STATE_OK;
    }    

    @Override
    public void setConnectionSettings(final ConnectionSettings connectionSettings) {
        this.connectionSettings = connectionSettings;
    }
    
    @Override
    public void postConstruct() {
        configuredWorkspacesTablePanel = new ConfiguredWorkspacesTablePanel(this);
        connectionService = new ConnectionService();
        configuredWorkspacesTablePanel.postConstruct();
    }
        
    public void add() {
        
    }

    public void remove() {
        
    }

    public void save() {
        
    }
    
    public Object[][] getWorkspacesData() {        
        final VersionControlClient vc = getVersionControlClient();
        
        if(workspaces.size() <= 0 || workspaces == null) {
            workspaces.addAll(Arrays.asList(vc.getRepositoryWorkspaces(null, connectionSettings.getUsername(), null)));
        }
        
        String[][] workspacesData = new String[workspaces.size()][4];
        for(int i = 0; i < workspaces.size(); i++) {
            Workspace workspace = workspaces.get(i);
            workspacesData[i][0] = workspace.getDisplayName();
            workspacesData[i][1] = workspace.getComputer();
            workspacesData[i][2] = workspace.getComment();            
            workspacesData[i][3] = workspace.getLastAccessDate().toString();
        }
        return workspacesData;
    }
    
    public VersionControlClient getVersionControlClient() {
        if(vcClient == null) {
            vcClient = connectionService.getTeamProjectCollection(connectionSettings).getVersionControlClient();
        } 
        return vcClient;
    }
    
    public TFSTeamProjectCollection getTeamProjectCollection() {
        return connectionService.getTeamProjectCollection(connectionSettings);
    }
        
    /**
     * Fields
     */     
    private List<Workspace> workspaces = new ArrayList<>();
    private static VersionControlClient vcClient = null;
    private String id;
    private TabPanelDescription multiViewDescription;
    private Lookup lookup;
    private TabPanelCallback multiViewPanelCallback;
    private ConnectionSettings connectionSettings;
    private ConfiguredWorkspacesTablePanel configuredWorkspacesTablePanel;
    private ConnectionService connectionService = new ConnectionService();
}
